﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace BasicForms
{
    public partial class FrmNotifier : BasicForms.FrmBase
    {
        
        #region Khai bao
        private static Rectangle RealTitleRectangle;
        private static Rectangle RealContentRectangle;
        private static Rectangle WorkAreaRectangle;
        private static Timer timer = new Timer();
        private static FormStates taskbarState = FormStates.hidden;
        private static int nShowEvents;
        private static int nHideEvents;
        private static int nVisibleEvents;
        private static int nIncrementShow;
        private static int nIncrementHide;
        private static bool bIsMouseOverPopup = false;
        private static bool bIsMouseOverClose = false;
        private static bool bIsMouseOverContent = false;
        private static bool bIsMouseOverTitle = false;
        private static bool bIsMouseDown = false;
        private static bool bKeepVisibleOnMouseOver = true;			// Added Rev 002
        private static bool bReShowOnMouseOver = false;	
        #endregion
        #region Instance property: Singleton Design pattern
        private static FrmNotifier _Instance;
        public static FrmNotifier Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new FrmNotifier();

                }
                return _Instance;
            }
        }
        #endregion
        public bool KeepVisibleOnMousOver
        {
            get
            {
                return bKeepVisibleOnMouseOver;
            }
            set
            {
                bKeepVisibleOnMouseOver = value;
            }
        }
      
        public bool ReShowOnMouseOver
        {
            get
            {
                return bReShowOnMouseOver;
            }
            set
            {
                bReShowOnMouseOver = value;
            }
        }
        #region Trang Thai Form
        public enum FormStates
		{
			hidden = 0,
			appearing = 1,
			visible = 2,
			disappearing = 3
		}
		#endregion

        public FrmNotifier()
        {
            InitializeComponent();
            //FormBorderStyle = FormBorderStyle.None;
            //WindowState = FormWindowState.Minimized;
            base.Show();
            base.Hide();
            WindowState = FormWindowState.Normal;
            ShowInTaskbar = false;
            TopMost = true;
            Height = 163;
            Width = 200;
            //MaximizeBox = false;
            //MinimizeBox = false;
            //ControlBox = false;
            
            timer.Enabled = true;
            timer.Tick += new EventHandler(OnTimer);
        }

        public static void ShowMessage(int x, int y, int width, int wide, string title, int milisecondToLive, string message)
        {

        }
        protected void OnTimer(Object obj, EventArgs ea)
        {
            switch (taskbarState)
            {
                case FormStates.appearing:
                    SetBounds(Left, Top - nIncrementShow, Width, Height + nIncrementShow);
                    //SetBounds(Left, Top , Width, Height);
                    timer.Stop();
                    timer.Interval = nVisibleEvents;
                    taskbarState = FormStates.visible;
                    timer.Start();             
                    break;

                case FormStates.visible:
                    timer.Stop();
                    timer.Interval = nHideEvents;
                    // Added Rev 002
                    if ((bKeepVisibleOnMouseOver && !bIsMouseOverPopup) || (!bKeepVisibleOnMouseOver))
                    {
                        taskbarState = FormStates.disappearing;
                    }
                    //taskbarState = TaskbarStates.disappearing;		// Rev 002
                    timer.Start();
                    break;

                case FormStates.disappearing:
                    // Added Rev 002
                    if (bReShowOnMouseOver && bIsMouseOverPopup)
                    {
                        taskbarState = FormStates.appearing;
                    }
                    else
                    {
                        if (Top < WorkAreaRectangle.Bottom)
                            SetBounds(Left, Top + nIncrementHide, Width, Height - nIncrementHide);
                        else
                            Hide();
                    }
                    break;
            }

        }
        public new void Hide()
        {
            if (taskbarState != FormStates.hidden)
            {
                timer.Stop();
                taskbarState = FormStates.hidden;
                base.Hide();
            }
        }
        [DllImport("user32.dll")]
        private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);
        public static void Show(string strTitle, string strContent, int nTimeToShow, int nTimeToStay, int nTimeToHide, int height, int width)
        {
            
          
                Instance.Height = height;
                Instance.Width = width;
                WorkAreaRectangle = Screen.GetWorkingArea(WorkAreaRectangle);
                Instance.Text = strTitle;
                Instance.lblContent.Text = strContent;
                nVisibleEvents = nTimeToStay;
                //CalculateMouseRectangles();

                // We calculate the pixel increment and the timer value for the showing animation
                int nEvents;
                if (nTimeToShow > 10)
                {
                    nEvents = Math.Min((nTimeToShow / 10), Instance.Height);
                    nShowEvents = nTimeToShow / nEvents;
                    nIncrementShow = Instance.Height / nEvents;
                }
                else
                {
                    nShowEvents = 10;
                    nIncrementShow = Instance.Height;
                }

                // We calculate the pixel increment and the timer value for the hiding animation
                if (nTimeToHide > 10)
                {
                    nEvents = Math.Min((nTimeToHide / 10), Instance.Height);
                    nHideEvents = nTimeToHide / nEvents;
                    nIncrementHide = Instance.Height / nEvents;
                }
                else
                {
                    nHideEvents = 10;
                    nIncrementHide = Instance.Height;
                }

                switch (taskbarState)
                {
                    case FormStates.hidden:
                        taskbarState = FormStates.appearing;
                        //Instance.SetBounds(WorkAreaRectangle.Right - Instance.Width - 17, WorkAreaRectangle.Bottom - 1, Instance.Width, 0);
                        Instance.SetBounds(WorkAreaRectangle.Right - Instance.Width - 17, WorkAreaRectangle.Bottom - Instance.Height - 1, Instance.Width, Instance.Height);
                        timer.Interval = nShowEvents;
                        timer.Start();
                        // We Show the popup without stealing focus
                        ShowWindow(Instance.Handle, 4);
                        break;

                    case FormStates.appearing:
                        Instance.Refresh();
                        break;

                    case FormStates.visible:
                        timer.Stop();
                        timer.Interval = nVisibleEvents;
                        timer.Start();
                        Instance.Refresh();
                        break;

                    case FormStates.disappearing:
                        timer.Stop();
                        taskbarState = FormStates.visible;
                        Instance.SetBounds(WorkAreaRectangle.Right - Instance.Width - 17, WorkAreaRectangle.Bottom - Instance.Height - 1, Instance.Width, Instance.Height);
                        timer.Interval = nVisibleEvents;
                        timer.Start();
                        Instance.Refresh();
                        break;
               
            }
                        
        }

        private void FrmNotifier_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }
       
    }
}
